package com.sdx.third;

import com.qcloud.Module.Vod;
import com.qcloud.QcloudApiModuleCenter;
import com.qcloud.vod.VodApi;
import com.qcloud.vod.response.VodUploadCommitResponse;
import com.sdx.common.utils.MD5;
import com.sdx.common.utils.Utils;

import java.util.TreeMap;

/**
 * @ClassName TencentVideoApi
 * @Description: 腾讯云点播api
 * @author: tanbaocai
 * @date: 2018/10/16 11:15
 */
public class TencentVideoApi {
	private static final String secretId = "AKIDjHaeS1dOCl48xaw95nGVVIrCJBpFrP9f";
	private static final String secretKey = "QcR1LPEnBKEsGOHvKTM1RbWCessdASWL";
	private static final String key = "yJ0mrwa3oxsb5tSaLz5v";

	public static VodApi getVodApi() {
		VodApi vodApi = new VodApi(secretId,secretKey);
		return vodApi;
	}

	public static VodUploadCommitResponse  uploadFile(String filePath) throws Exception {
		VodApi vodApi = getVodApi();
		VodUploadCommitResponse response = vodApi.upload(filePath);
		return  response;
	}

	/**
	 * 获取视频信息
	 * @param fileId
	 * @return
	 * @throws Exception
	 */
	public static String getVideoInfo(String fileId) throws Exception {
		TreeMap<String, Object> config = new TreeMap();
		config.put("SecretId", secretId);
		config.put("SecretKey", secretKey);
		config.put("RequestMethod", "GET");
		config.put("DefaultRegion", "gz");
		QcloudApiModuleCenter module = new QcloudApiModuleCenter(new Vod(), config);

		TreeMap<String,Object> param = new TreeMap<>();
		param.put("fileId",fileId);
		param.put("infoFilter.0","basicInfo");
		String result = module.call("GetVideoInfo", param);
		/**
		 * {
		 "code": 0,
		 "message": "",
		 "codeDesc": "Success",
		 "basicInfo": {
		 "name": "xx.mp4",
		 "size": 27304132,
		 "totalSize": 27304132,
		 "duration": 313,
		 "description": "",
		 "status": "normal",
		 "createTime": 1539675204,
		 "updateTime": 1539675205,
		 "expireTime": 0,
		 "classificationId": 0,
		 "classificationName": "其他",
		 "playerId": 133157,
		 "coverUrl": "",
		 "type": "mp4",
		 "sourceVideoUrl": "http://1256643784.vod2.myqcloud.com/199edf8dvodgzp1256643784/8685fcfb5285890782385186656/L3lhbQuRRG4A.mp4",
		 "tags": [ ],
		 "sourceInfo": null,
		 "sourceContext": "",
		 "region": "gzp",
		 "storageMode": 0,
		 "sourceType": 3,
		 "classificationPath": "其他"
		 }
		 }
		 */
		return result;
	}

	/**
	 * 生成防盗链播放地址
	 * @param srcUrl 源url
	 * @param t      链接有效期至，时间戳（秒）
	 * @param exper  试播时间，单位秒，0，表示完整播放
	 * @param rlimit 播放ip地址限制数量，0，表示无限制
	 * @return
	 */
	public static String getPlayUrl(String srcUrl,long t,int exper,int rlimit) {
		int start = srcUrl.indexOf("/",8);
		int end = srcUrl.lastIndexOf("/");
		String dir = srcUrl.substring(start,end+1);
		String us = Utils.getRandomStringByLength(12);
		String tStr = Long.toString(t,16);
		String iplimit = "";
		if (rlimit>0) {
			iplimit = Integer.toString(rlimit);
		}
		String sign = MD5.MD5Encode(key+dir+tStr+exper+iplimit+us);
		String url = srcUrl+"?t="+tStr+"&exper="+exper;
		if (rlimit>0) {
			url = url+"&rlimit="+iplimit;
		}
		url = url+"&us="+us+"&sign="+sign;
		return url;
	}
	public static void main(String[] args) throws Exception {

		String url = "https://1256643784.vod2.myqcloud.com/cdc68235vodtransgzp1256643784/e79354707447398156329461057/v.f30.mp4";
		long time = System.currentTimeMillis()/1000+300;
		System.out.println(getPlayUrl(url,time,0,3));

		//System.out.println(getVideoInfo("5285890783671908455"));

	}
	public static void main1(String[] args) {
		String url = "http://1256643784.vod2.myqcloud.com/cdc68235vodtransgzp1256643784/e79354707447398156329461057/v.f30.mp4";
		String dir = "/cdc68235vodtransgzp1256643784/e79354707447398156329461057/";
		String key = "yJ0mrwa3oxsb5tSaLz5v";
		String us = "3rkdfg98";
		String exper = "100";
		long time = System.currentTimeMillis()/1000+60*60*24*365*20;
		String t = Long.toString(time,16);
		//sign = md5(KEY + Dir + t + exper + rlimit + us)
		String sign = MD5.MD5Encode(key+dir+t+exper+us);
		//t，exper，rlimit，us，sign
		url = url+"?t="+t+"&exper="+exper+"&us="+us+"&sign="+sign;

		System.out.println(url);
	}
}
